<?php

namespace App\Game\Logic;

use App\Game\Core\AStrategy;
use App\Task\HuCardTask;

/**
 *  胡牌响应
 */
class HuCard extends AStrategy
{
    /**
     * 执行方法
     */
    public function exec()
    {
        $account = $this->_params['userinfo']['account'];
        $fd = $this->_params['userinfo']['fd'];
        $out_cards = $this->_params['data'];
        $game_conf = config('game');
        $room_data_key = sprintf($game_conf['user_room_data'], $out_cards['room_no']);
        if (redis()->exists($room_data_key)) {
            $room_data = redis()->hGetAll($room_data_key);
            if (!isset($room_data['is_game_over'])) {
                $this->cleanLoop($game_conf, $room_data['room_no']);
                $task = container()->get(HuCardTask::class);
                $task->doHu($room_data, $out_cards, $account, $fd);
            }
            if (isset($room_data['is_game_over']) && ($room_data['is_game_over'] == 0)) {
                $this->cleanLoop($game_conf, $room_data['room_no']);
                $task = container()->get(HuCardTask::class);
                $task->doHu($room_data, $out_cards, $account, $fd);
            }
        }
    }


    /**
     * @param $game_conf
     * @param $room_no
     */
    protected function cleanLoop($game_conf, $room_no)
    {
        $room_loop_key = sprintf($game_conf['user_room_loop'], $room_no);
        redis()->del($room_loop_key);
    }

}